package com.gitee.cashzhang27.test.boot.graphql.webflux.graphql.components;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gitee.cashzhang27.test.boot.graphql.webflux.graphql.ExecutionResultHandler;
import graphql.ExecutionResult;
import lombok.AllArgsConstructor;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

@Component
@AllArgsConstructor
public class DefaultExecutionResultHandler implements ExecutionResultHandler {

  private final ObjectMapper objectMapper;

  @Override
  public Object handleExecutionResult(Mono<ExecutionResult> executionResultMono,
      ServerHttpResponse serverHttpResponse) {
    return executionResultMono
        .map(executionResult -> handleImpl(executionResult, serverHttpResponse));
  }

  private Object handleImpl(ExecutionResult executionResult,
      ServerHttpResponse serverHttpResponse) {
    return executionResult.toSpecification();
  }
}
